package com.dao;

import com.pojo.ColumnInfo;
import com.pojo.TableInfo;
import com.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class GeneratorDao {

    // 所有表信息
    public List<TableInfo> getTables() {
        try (Connection connection = DBUtil.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select table_name tableName, table_comment tableComment " +
                    "from information_schema.tables " +
                    "where table_schema = (select database())");

            List<TableInfo> list = new ArrayList<>();
            while (resultSet.next()) {
                TableInfo tableInfo = new TableInfo();
                tableInfo.setTableName(resultSet.getString(1));
                tableInfo.setTableComment(resultSet.getString(2));
                list.add(tableInfo);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>(0);
    }

    public TableInfo getTableInfo(String tableName) {
        try (Connection connection = DBUtil.getConnection()) {
            PreparedStatement statement = connection.prepareStatement("select table_comment tableComment " +
                    "from information_schema.tables " +
                    "where table_schema = (select database()) and table_name = ?");
            statement.setString(1, tableName);
            ResultSet resultSet = statement.executeQuery();

            if (resultSet.next()) {
                TableInfo tableInfo = new TableInfo();
                tableInfo.setTableName(tableName);
                tableInfo.setTableComment(resultSet.getString(1));
                return tableInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 所有表的列信息
    public List<ColumnInfo> getColumnsOfTable(String tableName) {
        try (Connection connection = DBUtil.getConnection()) {
            PreparedStatement statement = connection.prepareStatement("select  column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra " +
                    "from information_schema.columns " +
                    "where table_name = ? and table_schema = (select database()) order by ordinal_position");
            statement.setString(1, tableName);
            ResultSet resultSet = statement.executeQuery();

            List<ColumnInfo> list = new ArrayList<>();
            while (resultSet.next()) {
                ColumnInfo info = new ColumnInfo();
                info.setColumnName(resultSet.getString(1));
                info.setDataType(resultSet.getString(2));
                info.setColumnComment(resultSet.getString(3));
                info.setColumnKey(resultSet.getString(4));
                info.setExtra(resultSet.getString(5));
                list.add(info);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>(0);
    }


//    public static void main(String[] args) {
//        new GeneratorDao().getColumnsOfTable("user");
//    }


}
